﻿/*
 ******************************************************************
 *      
 * 域名空间　	: GreatB2C.DALSQLServer
 * 类　名　称	: Admin.cs
 * 类　功　能	: 数据操作实现层
 * 类　流　程    :                             
 * 运用算法　	:                             
 * 编　写　人	: 王超             
 * 编写日期　	: 2011/3/28
 * 最近更新日期　:
 * 版　　本　	: V1.0                           
 * 描　　述　	: Descript of Class                                                 
 *                                        
 ******************************************************************
*/

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

using GreatB2C.Model;
using GreatB2C.DALFactory;
using GreatB2C.DALProfile;
using GreatB2C.Config;
using GreatB2C.Utility;

namespace GreatB2C.DALSQLServer
{
    /// <summary>
    /// 类描述：
    /// </summary>
    [Serializable]
    public class Admin : DbBase , IAdmin
    {
        string strSql = string.Empty;

        #region implement methods

        /// <summary>
        /// 获取获取参数参数列表
        /// </summary>
        /// <param name="ac"></param>
        /// <returns></returns>
        private SqlParameter[] GetParameters( GreatB2C.Model.Admin ac )
        {
            int i = 0;
            SqlParameter[] param = new SqlParameter[ 0 ];

            param[ i ] = new SqlParameter( "@AdminID" , SqlDbType.Int , 4 );
            param[ i++ ].Value = ac.AdminID;

            param[ i ] = new SqlParameter( "@AdminName" , SqlDbType.NVarChar , 50 );
            param[ i++ ].Value = ac.AdminName;

            param[ i ] = new SqlParameter( "@AdminPassword" , SqlDbType.NVarChar , 50 );
            param[ i++ ].Value = ac.AdminPassword;

            param[ i ] = new SqlParameter( "@UserName" , SqlDbType.NVarChar , 50 );
            param[ i++ ].Value = ac.UserName;

            param[ i ] = new SqlParameter( "@EnableMultiLogin" , SqlDbType.Bit , 1 );
            param[ i++ ].Value = ac.EnableMultiLogin;

            param[ i ] = new SqlParameter( "@RndPassword" , SqlDbType.NVarChar , 50 );
            param[ i++ ].Value = ac.RndPassword;

            param[ i ] = new SqlParameter( "@LoginTimes" , SqlDbType.Int , 4 );
            param[ i++ ].Value = ac.LoginTimes;

            param[ i ] = new SqlParameter( "@LastLoginIP" , SqlDbType.NVarChar , 50 );
            param[ i++ ].Value = ac.LastLoginIP;

            param[ i ] = new SqlParameter( "@LastLoginTime" , SqlDbType.DateTime , 8 );
            param[ i++ ].Value = ac.LastLoginTime;

            param[ i ] = new SqlParameter( "@LastLogoutTime" , SqlDbType.DateTime , 8 );
            param[ i++ ].Value = ac.LastLogoutTime;

            param[ i ] = new SqlParameter( "@LastModifyPasswordTime" , SqlDbType.DateTime , 8 );
            param[ i++ ].Value = ac.LastModifyPasswordTime;

            param[ i ] = new SqlParameter( "@IsLock" , SqlDbType.Bit , 1 );
            param[ i++ ].Value = ac.IsLock;

            param[ i ] = new SqlParameter( "@EnableModifyPassword" , SqlDbType.Bit , 1 );
            param[ i++ ].Value = ac.EnableModifyPassword;

            param[ i ] = new SqlParameter( "@Hash" , SqlDbType.NVarChar , 50 );
            param[ i++ ].Value = ac.Hash;

            param[ i ] = new SqlParameter( "@LoginErrorTimes" , SqlDbType.Int , 4 );
            param[ i++ ].Value = ac.LoginErrorTimes;


            return param;
        }

        /// <summary>
        /// 检查是否有重复记录。
        /// 注意：这里要手动修改一下，需要加入次主键做为第二条件（主要是为了修改操作）
        /// </summary>
        /// <param name="objModel">对应的数据映射类</param>
        /// <returns>操作结果：true-成功 false-失败</returns>
        public bool IsExists( GreatB2C.Model.Admin objModel )
        {
            string Sql = "SELECT Count(*) "
                            + " FROM "
                            + "[dbo].[" + Prefix + "Admin]"
                            + " WHERE "
                            + "	[AdminID] = @AdminID"
                //+" and 次主键=@次主键"
                            ;

            try
            {
                SqlParameter[] param = GetParameters( objModel );

                int count = Convert.ToInt32( DbHelper.ExecuteScalar( CommandType.Text , Sql , param ).ToString( ) );
                if ( count > 0 )
                {
                    return true;
                }

                return false;
            }
            catch ( Exception ex )
            {
                Console.Write( ex.Message );
                return false;
            }
        }

        /// <summary>
        /// 检索出表中所有记录	
        /// </summary>
        /// <param name="aryResult">返回对象数据集合</param>
        /// <returns>操作结果：true-成功 false-失败</returns>
        public DataTable ListAdmin( )
        {
            DataTable dtData = new DataTable( );
            string Sql = "SELECT "
                            + "[AdminID],"
                            + "[AdminName],"
                            + "[AdminPassword],"
                            + "[UserName],"
                            + "[EnableMultiLogin],"
                            + "[RndPassword],"
                            + "[LoginTimes],"
                            + "[LastLoginIP],"
                            + "[LastLoginTime],"
                            + "[LastLogoutTime],"
                            + "[LastModifyPasswordTime],"
                            + "[IsLock],"
                            + "[EnableModifyPassword],"
                            + "[Hash],"
                            + "[LoginErrorTimes]"
                            + " FROM "
                            + "[dbo].[" + Prefix + "Admin]";

            try
            {
                return DbHelper.ExecuteTable( CommandType.Text , Sql , null );
            }
            catch ( Exception ex )
            {
                Console.Write( ex.Message );
                return null;
            }
        }

        /// <summary>
        /// 根据表中主键ID查询记录信息
        /// </summary>
        /// <param name="objModel">相应的对象映射模型</param>
        /// <param name="DesModel">输出的对象映射模型</param>
        /// <returns>操作结果：true-成功 false-失败</returns>
        public GreatB2C.Model.Admin ListByID( GreatB2C.Model.Admin objModel )
        {
            string Sql = "SELECT * "
                            + " FROM "
                            + "[dbo].[" + Prefix + "Admin]"
                            + " WHERE "
                            + "	[AdminID] = @AdminID";

            try
            {
                SqlParameter[] param = GetParameters( objModel );

                using ( DataTable dtData = DbHelper.ExecuteTable( CommandType.Text , Sql , param ) )
                {
                    if ( dtData.Rows.Count > 0 )
                    {
                        GreatB2C.Model.Admin modClass = new GreatB2C.Model.Admin( );

                        modClass.AdminID = Convert.ToInt32( dtData.Rows[ 0 ][ "AdminID" ].ToString( ) );
                        modClass.AdminName = dtData.Rows[ 0 ][ "AdminName" ].ToString( );
                        modClass.AdminPassword = dtData.Rows[ 0 ][ "AdminPassword" ].ToString( );
                        modClass.UserName = dtData.Rows[ 0 ][ "UserName" ].ToString( );
                        modClass.EnableMultiLogin = Convert.ToBoolean( dtData.Rows[ 0 ][ "EnableMultiLogin" ].ToString( ) );
                        modClass.RndPassword = dtData.Rows[ 0 ][ "RndPassword" ].ToString( );
                        modClass.LoginTimes = Convert.ToInt32( dtData.Rows[ 0 ][ "LoginTimes" ].ToString( ) );
                        modClass.LastLoginIP = dtData.Rows[ 0 ][ "LastLoginIP" ].ToString( );
                        modClass.LastLoginTime = Convert.ToDateTime( dtData.Rows[ 0 ][ "LastLoginTime" ].ToString( ) );
                        modClass.LastLogoutTime = Convert.ToDateTime( dtData.Rows[ 0 ][ "LastLogoutTime" ].ToString( ) );
                        modClass.LastModifyPasswordTime = Convert.ToDateTime( dtData.Rows[ 0 ][ "LastModifyPasswordTime" ].ToString( ) );
                        modClass.IsLock = Convert.ToBoolean( dtData.Rows[ 0 ][ "IsLock" ].ToString( ) );
                        modClass.EnableModifyPassword = Convert.ToBoolean( dtData.Rows[ 0 ][ "EnableModifyPassword" ].ToString( ) );
                        modClass.Hash = dtData.Rows[ 0 ][ "Hash" ].ToString( );
                        modClass.LoginErrorTimes = Convert.ToInt32( dtData.Rows[ 0 ][ "LoginErrorTimes" ].ToString( ) );

                        return modClass;
                    }
                }

                return null;
            }
            catch ( Exception ex )
            {
                Console.Write( ex.Message );
                return null;
            }
        }

        /// <summary>
        /// 新增一条新记录到表中
        /// </summary>
        /// <param name="objModel">相应的对象映射模型</param>
        /// <returns>操作结果：true-成功 false-失败</returns>
        public bool Insert( GreatB2C.Model.Admin objModel )
        {
            string Sql = "INSERT INTO "
                        + "[dbo].[" + Prefix + "Admin]"
                        + "("
                        + "[AdminName],"
                        + "[AdminPassword],"
                        + "[UserName],"
                        + "[EnableMultiLogin],"
                        + "[RndPassword],"
                        + "[LoginTimes],"
                        + "[LastLoginIP],"
                        + "[LastLoginTime],"
                        + "[LastLogoutTime],"
                        + "[LastModifyPasswordTime],"
                        + "[IsLock],"
                        + "[EnableModifyPassword],"
                        + "[Hash],"
                            + "[LoginErrorTimes]"
                        + ") VALUES ("
                        + "@AdminName,"
                        + "@AdminPassword,"
                        + "@UserName,"
                        + "@EnableMultiLogin,"
                        + "@RndPassword,"
                        + "@LoginTimes,"
                        + "@LastLoginIP,"
                        + "@LastLoginTime,"
                        + "@LastLogoutTime,"
                        + "@LastModifyPasswordTime,"
                        + "@IsLock,"
                        + "@EnableModifyPassword,"
                        + "@Hash,"
                            + "@LoginErrorTimes"
                        + ")";

            try
            {
                SqlParameter[] param = GetParameters( objModel );

                int affect = DbHelper.ExecuteNonQuery( CommandType.Text , Sql , param );

                if ( affect == 1 )
                    return true;
                else
                    return false;
            }
            catch ( Exception ex )
            {
                Console.Write( ex.Message );
                return false;
            }
        }

        /// <summary>
        /// 根据主键ID更新指定记录
        /// </summary>
        /// <param name="objModel">相应的对象映射模型</param>
        /// <returns>操作结果：true-成功 false-失败</returns>
        public bool Update( GreatB2C.Model.Admin objModel )
        {
            string Sql = "UPDATE "
                        + "[dbo].[" + Prefix + "Admin]"
                        + " SET "
                        + "[AdminName] = @AdminName,"
                        + "[AdminPassword] = @AdminPassword,"
                        + "[UserName] = @UserName,"
                        + "[EnableMultiLogin] = @EnableMultiLogin,"
                        + "[RndPassword] = @RndPassword,"
                        + "[LoginTimes] = @LoginTimes,"
                        + "[LastLoginIP] = @LastLoginIP,"
                        + "[LastLoginTime] = @LastLoginTime,"
                        + "[LastLogoutTime] = @LastLogoutTime,"
                        + "[LastModifyPasswordTime] = @LastModifyPasswordTime,"
                        + "[IsLock] = @IsLock,"
                        + "[EnableModifyPassword] = @EnableModifyPassword,"
                        + "[Hash] = @Hash,"
                            + "[LoginErrorTimes] = @LoginErrorTimes"
                        + " WHERE "
                        + "[AdminID] = @AdminID"
                        ;

            try
            {
                SqlParameter[] param = GetParameters( objModel );

                int affect = DbHelper.ExecuteNonQuery( CommandType.Text , Sql , param );

                if ( affect == 1 )
                    return true;
                else
                    return false;
            }
            catch ( Exception ex )
            {
                Console.Write( ex.Message );
                return false;
            }
        }

        /// <summary>
        /// 根据主键删除指定记录
        /// </summary>
        /// <param name="objModel">相应的对象映射模型</param>
        /// <returns>操作结果：true-成功 false-失败</returns>
        public bool Delete( GreatB2C.Model.Admin objModel )
        {
            string Sql = "DELETE FROM "
                            + "[dbo].[" + Prefix + "Admin]"
                            + " WHERE "
                            + "[AdminID] = @AdminID"
                            ;

            try
            {
                SqlParameter[] param = GetParameters( objModel );

                int affect = DbHelper.ExecuteNonQuery( CommandType.Text , Sql , param );

                if ( affect == 1 )
                    return true;
                else
                    return false;
            }
            catch ( Exception ex )
            {
                Console.Write( ex.Message );
                return false;
            }
        }

        /// <summary>
        /// 根据ID列表批量删除记录
        /// 数据格式：1,2,3,4,5
        /// </summary>
        /// <param name="strList">ID列表</param>
        /// <returns>操作结果：true-成功 false-失败</returns>
        public bool DeleteList( string strList )
        {
            string Sql = "DELETE FROM "
                            + "[dbo].[" + Prefix + "Admin]"
                            + " WHERE "
                            + "[AdminID] in ('" + strList + "')"
                            ;

            try
            {
                int affect = DbHelper.ExecuteNonQuery( CommandType.Text , Sql , null );

                if ( affect == 1 )
                    return true;
                else
                    return false;
            }
            catch ( Exception ex )
            {
                Console.Write( ex.Message );
                return false;
            }
        }

        #endregion
    }
}
